<?php

require_once "core/plugin.abstract.php";

define('USER_NAME_SESSION', '__acl_user_name');
define('USER_ID_SESSION', '__acl_user_id');


class AuthUser
{
    public $user_name = "";
    public $user_id = NULL;

    public function AuthUser() {
    }
    public function isAnonymous() {
        return False;
    }
    public function updateByUserid($user_id) {
        return False;
    }
}

class AuthAnonymousUser extends AuthUser
{
    public function AuthAnonymousUser() {
        $this->user_name = "anonymous";
        $this->user_id = NULL;

    }
    public function isAnonymous() {
        return True;
    }

}

class AuthSimpleUser extends AuthUser
{
    public function AuthSimpleUser() {
        $this->user_name = "User0";
        $this->user_id = 0;
    }
    public function updateByUserid($user_id) {
        $this->user_name = "User".$user_id;
        $this->user_id = $user_id;
        return True;
    }

    # functions

    public static function login($user_name, $password) {
        if ($password == "simple") {
            $user = new AuthSimpleUser();
            $user->updateByUserid($user_name);
            AuthSimpleUser::setUserSession($user);
            return True;
        }
        return False;
    }
    public static function logout () {
        $front = FrontController::getInstance();
        $front->request()->removeSession(USER_NAME_SESSION);
        $front->request()->removeSession(USER_ID_SESSION);
    }
    public static function setUserSession ($user) {
        $front = FrontController::getInstance();
        $front->request()->setSession(USER_NAME_SESSION, $user->user_name);
        $front->request()->setSession(USER_ID_SESSION, $user->user_id);
        $front->response()->view()->assign("user", $user);
    }


}


class AuthPlugin extends Plugin_Abstract
{

    public function preDispatch() {
        $response = $this->response();
        $request = $this->request();
        // 防止出现错误进入无限dispache
        $request->setDispached(true);

        // 提取用户信息
        $user = NULL;
        if ($request->checkVar(USER_ID_SESSION, 'session')) {
            $user = new AuthSimpleUser();
            if ($user->updateByUserid($request->getVar(USER_ID_SESSION, 'session'))) {
                AuthSimpleUser::setUserSession($user);
            } else {
                $this->dispatchedLogin();
                return;
            }


        } else {
            // session中没有用户对象
            $user = new AuthAnonymousUser();
            $response->view()->assign("user", $user);

        }

        # 如果是匿名用户
        if ($user->isAnonymous()) {
            // index,error 不做权限管理
            if ($request->controller == 'index' || $request->controller == 'error' || $request->controller == NULL) {
                return;
            }
            # 不允许匿名
            //$this->dispatchedLogin();
            //return;
        }

    }

    public function dispatchedLogin() {
        $front = FrontController::getInstance();
        $this->request()->controller = "index";
        $this->request()->action = "login";
        $this->request()->setDispached(false);
    }

}



